#include <iostream>
#include <fstream>
#include "viml/compiler.h"
#include "viml/vm.h"

int main() {
    std::ifstream file("../test_debug.vim");
    std::string source((std::istreambuf_iterator<char>(file)),
                      std::istreambuf_iterator<char>());
    
    viml::Compiler compiler;
    auto result = compiler.compile(source, "test_debug.vim");
    
    if (!result.success) {
        std::cerr << "Compilation failed:\n";
        for (const auto& error : result.errors) {
            std::cerr << "  " << error << "\n";
        }
        return 1;
    }
    
    std::cout << "Bytecode dump:\n";
    for (size_t i = 0; i < result.function->bytecode.size(); ++i) {
        const auto& inst = result.function->bytecode[i];
        std::cout << i << ": " << static_cast<int>(inst.opcode) 
                  << " a=" << inst.a << " b=" << inst.b << " c=" << inst.c 
                  << " bx=" << inst.bx << "\n";
    }
    
    std::cout << "Constants:\n";
    for (size_t i = 0; i < result.function->constants.size(); ++i) {
        std::cout << i << ": " << result.function->constants[i].to_string() << "\n";
    }
    
    viml::VM vm;
    auto error = vm.execute(result.function);
    
    if (error != viml::ErrorCode::SUCCESS) {
        std::cerr << "Execution failed: " << static_cast<int>(error) << "\n";
        return 1;
    }
    
    return 0;
}